

memo = {}

def max_sub_sum(nums, i):
    if i in memo:
        return memo[i]

    if i == len(nums) - 1:
        return max(nums[i], 0)

    max_value = max(0, nums[i] + max_sub_sum(nums, i+1))
    memo[i] = max_value

    return max_value



if __name__ == '__main__':
    nums = [8, -4, -2, -1, 2, 6, -5, 4]
    print(max(max_sub_sum(nums, i) for i in range(len(nums))))

